import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../config/Constant.dart';
import '../../model/Bus.dart';
import '../../model/Types.dart';
import '../../res.dart';
import '../../utils/EventBus/EventBus.dart';
import '../../utils/Toast/MyTosat.dart';
import '../../utils/color/ColorUtils.dart';
import '../../utils/permission/PermissionUtils.dart';
import '../../utils/userbloc/user_bloc.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';
import '../convert/ToPDF.dart';
import '../loginregister/Login.dart';

class AddDialog extends StatefulWidget {
  const AddDialog({Key? key}) : super(key: key);

  @override
  State<AddDialog> createState() => _RenameState();
}

class _RenameState extends State<AddDialog> {
  late File file;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  Future<void> _selectFile() async {
    final result = await FilePicker.platform.pickFiles(
      type: FileType.custom,
      allowedExtensions: ['pdf'], // 允许选择的文件扩展名
    );

    if (result != null) {
      file = File(result.files.single.path!);
      // 在这里处理文件，例如读取文件内容或显示文件信息
      // print(result.files.single.identifier);
      EventBusUtil.fireEvent(Bus(Constant.ADD_FILE_MSG, file));

      // setState(() {});
    } else {
      // 处理用户取消选择文件的情况
    }
  }

  checkPermission() {
    PermissionUtils.checkReadWritePermission().then((status) {
      //   if (status) {
      _selectFile();
      // } else {
      //   Get.to(const Permissions());
      // }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(12)),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              const Spacer(),
              MyLocalImage(
                Res.actionsheet_icon_close3x,
                width: 24,
                height: 24,
                onclick: () {
                  Get.back();
                },
              ),
            ],
          ),
          const SizedBox(
            height: 20,
          ),
          InkWell(
            onTap: () async {
              Get.back();
              checkPermission();
            },
            child: Stack(
              children: [
                const MyLocalImage(
                  Res.plus_img_import,
                  height: 114,
                ),
                Positioned(
                    top: 24,
                    left: 24,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        TextView(
                          "imports".tr,
                          style:
                              TextStyle(color: ColorUtils.text, fontSize: 16),
                          textAlign: TextAlign.left,
                        ),
                        TextView(
                          "imports_1".tr,
                          style: TextStyle(color: Colors.black26),
                          margin: EdgeInsets.only(top: 8),
                          width: 180,
                          maxLines: 2,
                        ),
                      ],
                    ))
              ],
            ),
          ),
          const SizedBox(
            height: 16,
          ),
          InkWell(
            onTap: () {
              if (UserBlocTool.isLogin()) {
                Get.back();
                Types type = Types("convert".tr, Res.tool_icon_topdf, 2, ["PDF"]);
                Get.to(ConvertPDF(
                  type: type,
                ));
              } else {
                MyToast().show("please_log".tr, icon_tips: false);
                Get.to(const Login());
              }
            },
            child: Stack(
              children: [
                const MyLocalImage(
                  Res.plus_img_convert,
                  height: 114,
                ),
                Positioned(
                    top: 24,
                    left: 24,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        TextView(
                          "convert".tr,
                          style:
                              TextStyle(color: ColorUtils.text, fontSize: 16),
                          textAlign: TextAlign.left,
                        ),
                        TextView(
                          "convert_1".tr,
                          style: TextStyle(color: Colors.black26),
                          margin: EdgeInsets.only(top: 8),
                          width: 180,
                          maxLines: 2,
                        ),
                      ],
                    ))
              ],
            ),
          ),
          const SizedBox(
            height: 20,
          ),
        ],
      ),
    );
  }
}
